home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / src / gen / VQueue.hP < prev    next >
Text File  |  1992-04-14  |  3KB  |  131 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19.  
  20. #ifndef _<T>VQueue_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _<T>VQueue_h 1
  25.  
  26. #include "<T>.Queue.h"
  27.  
  28. class <T>VQueue : public <T>Queue
  29. {
  30. protected:
  31.   int                   size;
  32.   int                   cnt;
  33.   int                   inp;
  34.   int                   outp;
  35.   <T>*                  s;
  36.  
  37. public:
  38.  
  39.                         <T>VQueue(int sz = DEFAULT_INITIAL_CAPACITY);
  40.                         <T>VQueue(<T>VQueue&);
  41.                         ~<T>VQueue();
  42.  
  43.   void                  operator = (<T>VQueue&);
  44.  
  45.   void                  enq(<T&> item);
  46.   <T>                   deq();
  47.   <T>&                  front();
  48.   void                  del_front();
  49.  
  50.   int                   length();
  51.   int                   empty();
  52.   int                   full();
  53.  
  54.   int                   capacity();
  55.   void                  resize(int sz);
  56.   void                  clear();
  57.  
  58.   int                   OK();
  59. };
  60.  
  61.  
  62. inline <T>VQueue::<T>VQueue(int sz)
  63. {
  64.   s = new <T> [size = sz];
  65.   cnt = inp = outp = 0;
  66. }
  67.  
  68. inline <T>VQueue::~<T>VQueue()
  69. {
  70.   delete [] s;
  71. }
  72.  
  73. inline void <T>VQueue::clear()
  74. {
  75.   inp = outp = 0;
  76.   cnt = 0;
  77. }
  78.  
  79. inline int <T>VQueue::empty()
  80. {
  81.   return cnt <= 0;
  82. }
  83.  
  84. inline int <T>VQueue::capacity()
  85. {
  86.   return size;
  87. }
  88.  
  89. inline int <T>VQueue::full()
  90. {
  91.   return cnt >= size;
  92. }
  93.  
  94. inline int <T>VQueue::length()
  95. {
  96.   return cnt;
  97. }
  98.  
  99. inline void <T>VQueue::enq(<T&> item)
  100. {
  101.   if (cnt >= size) error("enq to full Queue.");
  102.   ++cnt;
  103.   s[inp] = item;
  104.   if (++inp == size) inp = 0;
  105. }
  106.  
  107. inline <T> <T>VQueue::deq()
  108. {
  109.   if (cnt <= 0) error("deq from empty Queue.");
  110.   --cnt;
  111.   int i = outp;
  112.   if (++outp == size) outp = 0;
  113.   return s[i];
  114. }
  115.  
  116.  
  117. inline void <T>VQueue::del_front()
  118. {
  119.   if (cnt <= 0) error("delete from empty Queue.");
  120.   --cnt;
  121.   if (++outp == size) outp = 0;
  122. }
  123.  
  124. inline <T>& <T>VQueue::front()
  125. {
  126.   if (empty()) error("top from empty Queue.");
  127.   return s[outp];
  128. }
  129.  
  130. #endif
  131.